library ShowTeamMedalCountCommand initializer init requires LegacySystem

	private function GetMedalCount takes player p returns integer  
		local integer i = 1
		local integer rval = 0
		
		loop
			exitwhen i>13
			if i==11 then
				set i=13 //DI Ribbon
			endif
				
			if GetPlayerTechCount(p, udg_Medal[i], true) > 0 then
				set rval = rval + 1
			endif
			set i=i+1
		endloop
		
		return rval
	endfunction
	
	private function MyMedals takes nothing returns boolean
		local integer i
		local player p = GetTriggerPlayer()
		local integer count = 0
		local integer vacancy = 0
		
		call DisplayTimedTextToPlayer(p, 0, 0, 20.0, " " )
		call DisplayTimedTextToPlayer(p, 0, 0, 20.0, "|cfff3ad00Decorations awarded to Echo Company marines|r")
		set i = 0
		loop
			exitwhen i > 11
			if GetPlayerSlotState(Player(i)) != PLAYER_SLOT_STATE_EMPTY and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
				set count = GetMedalCount(Player(i))
				call DisplayTimedTextToPlayer(p,0,0, 20.0, GetPlayerName(Player(i)) + " has been awarded |cfff3ad00" + I2S(count) + "|r medals" )
			else
				set vacancy = vacancy + 1
			endif

			set i = i + 1
		endloop
		
		loop
			exitwhen 0 >= vacancy
			call DisplayTimedTextToPlayer(p, 0.0, 0.0, 20.0, " " )
			set  vacancy = vacancy  - 1
		endloop
	
		
		call DisplayTimedTextToPlayer(p, 0, 0, 20.0, " " )
		
		set p = null
		return false
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		local integer i = 0
		set t = CreateTrigger()
		loop
			exitwhen i > 11
			call TriggerRegisterPlayerChatEvent(t, Player(i), "-medals", true)
			set i = i + 1
		endloop
		call TriggerAddCondition(t, Condition(function MyMedals))
	endfunction
endlibrary

